<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class IBase {
	var $id;
	var $name;
	var $description;
	var $value;
	
	var $date_added;
	var $added_by;
	var $last_modified;
	var $modified_by;
	var $sort_order;
	var $status;
	
	var $_id;
	var $_key;
	var $_fields;
	
	//constructor
	function IBase() {
		
	}
	
	function setTable($table) {
		$this->_table = $table;
	}
	
	function getTable() {
		return $this->_table;
	}
	
	function setKey($key) {
		$this->_key = $key;
	}
	
	function getKey() {
		return $this->_key;
	}
	
	function setFields($fields) {
		$this->_fields = $fields;
	}
	
	function getFields() {
		return $this->_fields;
	}
	
	function setID($id) {
		$this->_id = $id;
	}
	
	function getID() {
		return $this->_id;
	}
	
	//
	function setName($name) {
		$this->name = $name;
	}
	
	function getName() {
		return $this->name;
	}
	
	function setDateAdded($date_added) {
		$this->date_added = $date_added;
	}
	
	function getDateAdded() {
		return $this->date_added;
	}
	
	function setAddedBy($added_by) {
		$this->added_by = $added_by;
	}
	
	function getAddedBy() {
		return $this->added_by;
	}
	
	function setLastModified($last_modified) {
		$this->last_modified = $last_modified;
	}
	
	function getLastModified() {
		return $this->last_modified;
	}
	
	function setModifiedBy($modified_by) {
		$this->modified_by = $modified_by;
	}
	
	function getModifiedBy() {
		return $this->modified_by;
	}
	
	function setStatus($status) {
		$this->status = $status;
	}
	
	function getStatus() {
		return $this->status;
	}
	
	//
	function bind($data_array) {
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$this->$k = $v;
				$this->_fields[] = $k;
			}
		}
	}
	
	function to() {
		
	}
	
	function toString() {
		
	}
	
	function toArray() {
		$b = array();
		$a = get_object_vars($this);
		foreach($a as $k => $v) {
			if( substr($k, 0, 1) != '_' ) {
				$b[$k] = $v;
			}
		}
		
		return $b;
	}
}

//
$base_owner = -1;

//
class Base {
	var $adb;
	var $owner;
	var $languages_id;
	
	var $query_params;
	var $query_limit_start;
	var $query_limit_length;
	var $query_limit_start_last;
	var $query_limit_length_last;
	var $query_languages_id;
	var $query_columns;
	var $query_columns_used = false;
	
	var $cache_owner = true;
	var $cache_expiry = 0;
	var $cache_reset = true;
	
	//constructor
	function Base($owner = -1) {
		global $adb;
		global $base_owner;
		
		$this->adb =& $adb;
		if( $owner == -1 ) {
			if( isset($base_owner) && $base_owner >= 0 ) {
				$this->owner = $base_owner;
			} else {
				$this->owner = -1;
			}
		} else {
			$this->owner = $owner;
		}
		//global $SYS_RT_SETTINGS;
		//$this->languages_id = $SYS_RT_SETTINGS['SYS_RT_LANGUAGEID'];
		$this->languages_id = '1';
		
		$this->query_params = array();
		$this->query_limit_start = 0;
		$this->query_limit_length = 10;
		$this->query_languages_id = '1';
		$this->query_limit_start_last = '';
		$this->query_limit_length_last = '';
	}
	
	function init() {
		
	}
	
	function setADB($adb) {
		$this->adb = $adb;
	}
	
	function getADB() {
		return $this->adb;
	}
	
	function setOwner($owner) {
		$this->owner = $owner;
	}
	
	function setCurrentOwner() {
		$this->owner = $this->_get_current_user();
	}
	
	function getOwner() {
		return $this->owner;
	}
	
	function setLanguageID($languages_id = '') {
		if( $languages_id == '' ) {
			//global $SYS_RT_SETTINGS;
			//$this->languages_id = $SYS_RT_SETTINGS['SYS_RT_LANGUAGEID'];
			$this->languages_id = '1';
		} else {
			$this->languages_id = $languages_id;
		}
	}
	
	function getLanguageID() {
		return $this->languages_id;
	}
	
	//
	function setQueryParam($key, $value) {
		if( is_array($value) ) {
			$this->query_params[$key] = $value;
		} else {
			$this->query_params[$key][] = $value;
		}
	}
	
	function getQueryParam($key) {
		return $this->query_params[$key];
	}
	
	function getQueryParamStr($type = 'field') {
		$query_str = "";
		if( $type == 'field' ) {
			if( isset($this->query_params[$type]) ) {
				if( is_array($this->query_params[$type]) ) {
					foreach($this->query_params[$type] as $k => $v) {
						if( is_numeric($k) ) {
							$query_str .= $v . ", ";
						} else {
							$query_str .= $v . " as " . $k . ", ";
						}
					}
					$query_str = substr($query_str, 0, -2);
				} else {
					$query_str = $this->query_params[$type];
				}				
				if( $query_str == "" ) {
					$query_str = " * ";
				}
			}
		} elseif( $type == 'table' ) {
			if( isset($this->query_params[$type]) ) {
				if( is_array($this->query_params[$type]) ) {
					foreach($this->query_params[$type] as $k => $v) {
						if( is_numeric($k) ) {
							$query_str .= $v . ", ";
						} else {
							$query_str .= $v . " " . $k . ", ";
						}
					}
					$query_str = substr($query_str, 0, -2);
				} else {
					$query_str = $this->query_params[$type];
				}
				
				//$query_str = " from " . $query_str;
			}
		} elseif( $type == 'where' ) {
			if( isset($this->query_params[$type]) ) {
				if( is_array($this->query_params[$type]) ) {
					foreach($this->query_params[$type] as $k => $v) {
						if( is_numeric($k) ) {
							$query_str .= $v . ", ";
						} else {
							$query_str .= $v . " " . $k . ", ";
						}
					}
					$query_str = substr($query_str, 0, -2);
				} else {
					$query_str = $this->query_params[$type];
				}
				
				$query_str = " where " . $query_str;
			}
		} elseif( $type == 'order' ) {
			if( isset($this->query_params[$type]) ) {
				if( is_array($this->query_params[$type]) ) {
					foreach($this->query_params[$type] as $k => $v) {
						if( is_numeric($k) ) {
							$query_str .= $v . ", ";
						} else {
							$query_str .= $v . " " . $k . ", ";
						}
					}
					$query_str = substr($query_str, 0, -2);
				} else {
					$query_str = $this->query_params[$type];
				}
				
				$query_str = " order by " . $query_str;
			}
		} elseif( $type == 'group' ) {
			if( isset($this->query_params[$type]) ) {
				$query_str = " group by " . $this->query_params[$type];
			}
		}
		
		return $query_str;
	}
	
	function setQueryLimitStart($limit_start = '0') {
		if( is_numeric($limit_start) && $limit_start > 0 ) {
			$this->query_limit_start = $limit_start;
		} else {
			$this->query_limit_start = 0;
		}
	}
	
	function getQueryLimitStart() {
		return $this->query_limit_start;
	}
	
	function setQueryLimitLength($limit_length = '10') {
		if( is_numeric($limit_length) && $limit_length > 0 ) {
			$this->query_limit_length = $limit_length;
		} else {
			$this->query_limit_length = 'INFINITE';
		}
	}
	
	function getQueryLimitLength() {
		return $this->query_limit_length;
	}
	
	function setQueryNoLimit() {
		$this->query_limit_length = 'INFINITE';
	}
	
	function getQueryLimitStr() {
		$query_limit_str = "";
		if( $this->query_limit_length == 'INFINITE' ) {
			//$query_limit_str = " limit " . $this->query_limit_start;
			$query_limit_str = " limit 1000";
		} else {
			$query_limit_str = " limit " . $this->query_limit_start . ", " . $this->query_limit_length;
		}
		
		return $query_limit_str;
	}
	
	function setQueryLanguagesID($query_languages_id = '') {
		if( $languages_id == '' ) {
			//global $SYS_RT_SETTINGS;
			//$this->query_languages_id = $SYS_RT_SETTINGS['SYS_RT_LANGUAGEID'];
			$this->query_languages_id = '1';
		} else {
			$this->query_languages_id = $query_languages_id;
		}
	}
	
	function getQueryLanguagesID() {
		return $this->query_languages_id;
	}
	
	function setQueryColumns($query_columns) {
		$this->query_columns = $query_columns;
	}
	
	function getQueryColumns() {
		if( $this->query_columns_used == true ) {
			return ' * ';
		}
		$this->query_columns_used = true;
		return $this->query_columns;
	}
	
	function setQueryOrderSort() {
		if( isset($_GET['orderby']) ) {
			$this->query_order_by = $_GET['orderby'];
		}
		if( isset($_GET['ordersort']) ) {
			$this->query_order_sort = $_GET['ordersort'];
		}
	}
	
	//
	function logQueryLimit() {
		$this->query_limit_start_last = $this->query_limit_start;
		$this->query_limit_length_last = $this->query_limit_length;
	}
	
	function resetQueryLimit() {
		$this->query_limit_length = $this->query_limit_length_last;
		$this->query_limit_start = $this->query_limit_start_last;
		$this->query_limit_start_last = '';
		$this->query_limit_length_last = '';
	}
	
	//
	function getFields($table) {
		global $filecacheObj;
			
		$prefix = 'database_table';
		$key = $table;
		$data = $filecacheObj->get($key, $prefix);
		if( $filecacheObj->cache_success == true ) {
			$filecacheObj->cache_success = false;
		} else {
			$data = $this->adb->getFields($table);
			$filecacheObj->set($key, $data, $prefix);
		}
		
		return $data;
	}
	
	function getFieldsA($table) {
		global $filecacheObj;
			
		$prefix = 'database_table';
		$key = $table;
		$data = $filecacheObj->get($key, $prefix);
		if( $filecacheObj->cache_success == true ) {
			$filecacheObj->cache_success = false;
		} else {
			$data = $this->adb->getFieldsA($table);
			$filecacheObj->set($key, $data, $prefix);
		}
		
		return $data;
	}
	
	//
	function getField($table, $id, $field, $pk = '') {
		if( $pk == '' ) {
			$pk = (substr($table, 0, 3) == 'pp_' ? substr($table, 3) : $table) . '_id';
		}
		$sql = "select " . $field . " from " . $table . " where " . $pk . " = '" . $id . "'";
		$row = $this->adb->getRow($sql);
		if( is_array($row) && isset($row[$field]) ) {
			return $row[$field];
		}
		
		return '';
	}
	
	function updateField($table, $id, $field, $value, $pk = '') {
		if( $pk == '' ) {
			$pk = ((substr($table, 0, 3) == 'pp_') ? substr($table, 3) : $table) . '_id';
		}
		$sql = "update " . $table . " set " . $field . " = '" . func_db_input($value) . "' where " . $pk . " = '" . $id . "'";
		return $this->adb->update($sql);
	}
	
	function updateFieldC($table, $id, $field, $value, $pk = '') {
		if( $pk == '' ) {
			$pk = $table . '_id';
		}
		$count = $this->adb->getCountA($table, " " . $pk . " != '" . $id . "' and " . $field . " = '" . func_db_input($value) . "'");
		if( $count > 0 ) {
			return -1;
		} else {
			return $this->adb->updateField($table, $id, $field, $value, $pk);
		}
	}
	
	//
	function checkFieldOnly($table, $field, $value, $id = 0, $pk = '', $where = '') {
		if( $pk == '' ) {
			$pk = ((substr($table, 0, 3) == 'pp_') ? substr($table, 3) : $table) . '_id';
		}
		$query = "select count(*) as count from " . $table . " where " . $field . " = '" . func_db_input($value) . "'";
		if( $id > 0 && $pk != '' ) {
			$query .= " and " . $pk . " <> '" . (int)$id . "'";
		}
		if( $where != '' ) {
			$query .= " and " . $where;
		}
		
		return $this->adb->checkCount($query);
	}
	
	function checkFieldCount($table, $field, $value, $id = 0, $pk = '', $where = '') {
		if( $pk == '' ) {
			$pk = ((substr($table, 0, 3) == 'pp_') ? substr($table, 3) : $table) . '_id';
		}
		$query = "select count(*) as count from " . $table . " where " . $field . " = '" . func_db_input($value) . "'";
		if( $id > 0 && $pk != '' ) {
			$query .= " and " . $pk . " <> '" . (int)$id . "'";
		}
		if( $where != '' ) {
			$query .= " and " . $where;
		}
		
		return $this->adb->getCount($query);
	}
	
	//
	function _insert($table, $data_array) {
		if( empty($table) || !is_array($data_array) ) return -1;
		$data = array();
		$_fields = $this->getFieldsA($table);
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				$data[$k] = $v;
			}
		}
		if( is_array($data) && sizeof($data) > 0 ) {
			return $this->adb->insertA($table, $data);
		}
		
		return 0;
	}
	
	function _add($table, $data_array) {
		if( empty($table) || !is_array($data_array) ) return -1;
		$data = array();
		$_fields = $this->getFieldsA($table);
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				$data[$k] = $v;
			}
		}
		if( is_array($data) && sizeof($data) > 0 ) {
			return $this->adb->addA($table, $data);
		}
		
		return false;
	}
	
	function _update($table, $data_array, $parameters) {
		if( empty($table) || empty($parameters) || !is_array($data_array) ) return -1;
		$data = array();
		$_fields = $this->getFieldsA($table);
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				$data[$k] = $v;
			}
		}
		if( is_array($data) && sizeof($data) > 0 ) {
			return $this->adb->updateA($table, $data, $parameters);
		}
		
		return false;
	}
	
	function _update_in($table, $data_array, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) || !is_array($data_array) ) return -1;
		if( $where != '' ) $where = " and " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		$data = array();
		$_fields = $this->getFieldsA($table);
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				$data[$k] = $v;
			}
		}
		if( is_array($data) && sizeof($data) > 0 ) {
			$parameters = "$key $type (" . $this->_implode(',', $values) . ")" . $where;
			return $this->adb->updateA($table, $data, $parameters);
		}
		
		return false;
	}
	
	function _update_ins($table, $data_array, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) || empty($data_array) ) return -1;
		if( !is_array($data_array) ) {
			return $this->_update_insd($table, $data_array, $key, $values, $where);
		}
		if( $where != '' ) $where = " and " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		$data = array();
		$_fields = $this->getFieldsA($table);
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				$data[$k] = $v;
			}
		}
		if( is_array($data) && sizeof($data) > 0 ) {
			if( sizeof($values) > 30 ) {
				$values = array_values($values);
				for($i=0, $n=sizeof($values); $i<$n; $i+=30) {
					$vs = array_slice($values, $i, 30);
					$parameters = "$key $type (" . $this->_implode(',', $vs) . ")" . $where;
					$r = $this->adb->updateA($table, $data, $parameters);
				}
				return true;
			} else {
				$parameters = "$key $type (" . $this->_implode(',', $values) . ")" . $where;
				return $this->adb->updateA($table, $data, $parameters);
			}
		}
		
		return false;
	}
	
	function _update_insd($table, $data, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) || empty($data) ) return -1;
		if( $where != '' ) $where = " and " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		if( sizeof($values) > 30 ) {
			$values = array_values($values);
			for($i=0, $n=sizeof($values); $i<$n; $i+=30) {
				$vs = array_slice($values, $i, 30);
				$parameters = "$key $type (" . $this->_implode(',', $vs) . ")" . $where;
				$query = "update " . $table . " set " . $data . " where " . $parameters;
				$r = $this->adb->update($query);
			}
			return true;
		} else {
			$parameters = "$key $type (" . $this->_implode(',', $values) . ")" . $where;
			$query = "update " . $table . " set " . $data . " where " . $parameters;
			return $this->adb->update($query);
		}
		
		return false;
	}
	
	function _delete($table, $key, $value, $where = '') {
		if( empty($table) || empty($key) || empty($value) ) return -1;
		return $this->adb->delete("delete from " . $table . " where $key = '" . $value . "'");
	}
	
	function _deletes($table, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) ) return -1;
		if( $where != '' ) $where = " and " . $where;
		$where = " " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		return $this->adb->delete("delete from " . $table . " where $key " . $type . " (" . $this->_implode(',', $values) . ")" . $where);
	}
	
	function _delete_in($table, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) ) return -1;
		if( $where != '' ) $where = " and " . $where;
		$where = " " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		return $this->adb->delete("delete from " . $table . " where $key " . $type . " (" . $this->_implode(',', $values) . ")" . $where);
	}
	
	function _delete_ins($table, $key, $values, $where = '', $type = 'in') {
		if( empty($table) || empty($key) || empty($values) ) return -1;
		if( $where != '' ) $where = " and " . $where;
		$where = " " . $where;
		if( $type != 'not in' || $type != 'in' ) $type = 'in';
		if( sizeof($values) > 30 ) {
			$values = array_values($values);
			for($i=0, $n=sizeof($values); $i<$n; $i+=30) {
				$vs = array_slice($values, $i, 30);
				$this->adb->delete("delete from " . $table . " where $key " . $type . " (" . $this->_implode(',', $vs) . ")" . $where);
			}
		} else {
			return $this->adb->delete("delete from " . $table . " where $key " . $type . " (" . $this->_implode(',', $values) . ")" . $where);
		}
	}
	
	function _delete_exs($table, $key, $values, $where = '') {
		return $this->_delete_ins($table, $key, $values, $where, 'not in');
	}
	
	// xxx_to_yyy
	function _add2($table, $keys, $values) {
		if( is_array($keys) && is_array($values) ) {
			$data_array = array();
			for($i=0, $n=sizeof($keys); $i<$n; $i++) {
				$data_array[$keys[$i]] = $values[$i];
			}
			return $this->_add($table, $data_array);
		} else {
			return $this->adb->add("insert into " . $table . "(" . $keys . ") values (" . $values . ")");
		}
	}
	
	function _add2b($table, $key, $parameters) {
		
	}
	
	//
	function _add_once($table, $field, $parameters) {
		$query = "update " . $table . " set " . $field . " = '" . $field . " + 1 where " . $parameters;
		return $this->adb->update($query);
	}
	
	//log
	function addLog($subject, $content = '') {
		$this->_log = array(
				'logs_subject' => $subject,
				'logs_content' => $content,
					);
	}
	
	function commitLog($content = '') {
		if( $this->_log == '' && !is_array($this->_log) ) {
			return false;
		}
		$data_array = $this->_log;
		if( $content != '' ) {
			$data_array['logs_content'] = $content;
		}
		$data_array['logs_uid'] = $this->_get_current_user();
		$data_array['logs_time'] = 'now()';
		
		$result = $this->adb->insertA('core_logs', $data_array);
		$this->_log = '';
		
		return $result;
	}
	
	function addLogB($subject, $content) {
		
	}
	
	function logg() {
		
	}
	
	//transaction
	function startTransaction() {
		
	}
	
	function endTransaction() {
		
	}
	
	function commitTransaction() {
		$this->adb->commit();
	}
	
	function rollbackTransaction() {
		$this->adb->rollback();
	}
	
	//
	function error($msg, $stop = 1) {
		echo $msg;
		exit();
	}
	
	//lang
	function lang($key, $module = '', $load = false) {
		if( $module != '' ) {
			func_lang_dynamic_load_package($module);
		}
		global $LANGUAGE;
		if( strstr($key, ':') != false ) {
			$a = explode(':', $key);
			$key = $a['1'];
			$module = $a['0'];
			
		} elseif( $module == '' ) {
			return $key;
		}
		if( isset($LANGUAGE[$module][$key]) ) {
			return $LANGUAGE[$module][$key];
		}
		
		return $key;
	}
	
	//
	function load($file, $type = 'class') {
		if( $type == 'class' && $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $file);
		} elseif( $type == 'function' && $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . $file);
		}
	}
	
	function loadclass($classfile) {
		if( file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $classfile) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $classfile);
		}
	}
	
	//
	function &getObj($class, $file = '') {
		if( $file != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $file) ) {
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $file);
		}
		if( class_exists($class) ) {
			$obj = new $class();
			$obj->init();
			
			return $obj;
		}
		
		return NULL;
	}
	
	//call
	//	obj, class name or class object
	function call($obj, $method, $params = '', $type = '_array_') {
		if( empty($obj) ) {
			return -1;
		}
		if( !is_object($obj) ) {
			$obj = $this->getObj($obj);
			if( $obj == NULL ) {
				return -2;
			}
		}
		if( method_exists($obj, $method) ) {
			if( $type != '_array_' ) {
				$paramsb = array();
				$paramsb[] = $params;
				$paramsb[] = $type;
				$args = func_get_args();
				for($i=4, $n=sizeof($args); $i<$n; $i++) {
					$paramsb[] = $args[$i];
				}
				$params = $paramsb;
			} elseif( !is_array($params) ) {
				$paramsb = array();
				$paramsb[] = $params;
				$params = $paramsb;
			}
			//return call_user_func(array($obj, $method), $params);
			if( sizeof($params) == 0 ) {
				$data = $obj->$method();
			} elseif( sizeof($params) == 1 ) {
				$data = $obj->$method($params['0']);
			} elseif( sizeof($params) == 2 ) {
				$data = $obj->$method($params['0'], $params['1']);
			} elseif( sizeof($params) == 3 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2']);
			} elseif( sizeof($params) == 4 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3']);
			} elseif( sizeof($params) == 5 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4']);
			} elseif( sizeof($params) == 6 ) {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5']);
			} else {
				$data = $obj->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5'], $params['6']);
			}
			
			return $data;
		}
		
		return -3;
	}
	
	//cache
	function cache() {
		$args = func_get_args();
		$method = $args['0'];
		$params = array();
		if( isset($args['1']) ) {
			$params = $args['1'];
			if( !is_array($params) ) {
				$params = array($params);
			}
		}
		if( method_exists($this, $method) ) {
			global $filecacheObj;
			
			$prefix = get_class($this) . '_' . $method;
			if( $this->cache_owner == false ) {
				$key = $filecacheObj->_get_array_key($params) . '___' . $filecacheObj->_get_obj_key($this, array('adb', 'owner'));
			} else {
				$key = $filecacheObj->_get_array_key($params) . '___' . $filecacheObj->_get_obj_key($this, 'adb');
			}
			
			//time
			if( $this->cache_expiry > 0 ) {
				$data = $filecacheObj->getT($key, $prefix, $this->cache_expiry);
				if( $this->cache_reset == true ) {
					$this->cache_expiry = 0;
				}
			} else {
				$data = $filecacheObj->get($key, $prefix);
			}
			if( $filecacheObj->cache_success == true ) {
				$filecacheObj->cache_success = false;
			} else {
				if( sizeof($params) == 0 ) {
					$data = $this->$method();
				} elseif( sizeof($params) == 1 ) {
					$data = $this->$method($params['0']);
				} elseif( sizeof($params) == 2 ) {
					$data = $this->$method($params['0'], $params['1']);
				} elseif( sizeof($params) == 3 ) {
					$data = $this->$method($params['0'], $params['1'], $params['2']);
				} elseif( sizeof($params) == 4 ) {
					$data = $this->$method($params['0'], $params['1'], $params['2'], $params['3']);
				} elseif( sizeof($params) == 5 ) {
					$data = $this->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4']);
				} elseif( sizeof($params) == 6 ) {
					$data = $this->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5']);
				} else {
					$data = $this->$method($params['0'], $params['1'], $params['2'], $params['3'], $params['4'], $params['5'], $params['6']);
				}
				$filecacheObj->set($key, $data, $prefix);
			}
		
			return $data;
		}
		
		return '';
	}
	
	function cacheclean($method = '', $params = '') {
		global $filecacheObj;
		
		$prefix = $key = '';
		if( $method == '' ) {
			$prefix = get_class($this);
		} elseif( method_exists($this, $method) ) {
			$prefix = get_class($this) . '_' . $method;
		}
		if( $params != '' && is_array($params) ) {
			if( $this->cache_owner == false ) {
				$key = $filecacheObj->_get_array_key($params) . '___' . $filecacheObj->_get_obj_key($this, array('adb', 'owner'));
			} else {
				$key = $filecacheObj->_get_array_key($params) . '___' . $filecacheObj->_get_obj_key($this, 'adb');
			}
		}
		
		//
		$filecacheObj->clean($prefix, $key);
	}
	
	function setcachetime($time = '') {
		if( $time == 'hour' ) {
			$this->cache_expiry = 3600;
		} elseif( $time == '2hour' ) {
			$this->cache_expiry = 7200;
		} elseif( $time == 'halfday' ) {
			$this->cache_expiry = 43200;
		} elseif( $time == 'day' ) {
			$this->cache_expiry = 86400;
		} elseif( $time == 'week' ) {
			$this->cache_expiry = 2592000;
		} elseif( $time == 'month' ) {
			$this->cache_expiry = 999999999;
		} elseif( intval($time) > 0 ) {
			$this->cache_expiry = intval($time);
		} else {
			$this->cache_expiry = 3600;
		}
	}
	
	function setcachereset($reset = true) {
		$this->cache_reset = ($reset == true) ? true : false;
	}
	
	function cachedb() {
		
	}
	
	function setcachetype($type = 'file') {
		if( $type == 'file' || $type == 'db' || $type == 'momcache' ) {
			$this->cachetype = $type;
		}
	}
	
	function getcachetype() {
		return $this->cachetype;
	}
	
	//
	function AlignLength($a, $b) {
		$s = '';
		if( strlen($a) < $b ) {
			for($i=0, $n=$b-sizeof($a); $i<$n; $i++) {
				$s .= '0';
			}
			$s .= $a;
		} else {
			$s = $a;
		}
		
		return $s;
	}
	
	//
	function getListItem($data_array, $key, $id) {
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			if( $data_array[$i][$key] == $id ) {
				return $data_array[$i];
			}
		}
		
		return NULL;
	}
	
	function getListKey($data_array, $key) {
		$ids = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$ids[] = $data_array[$i][$key];
		}
		
		return $ids;
	}
	
	function getListKeys($data_array, $key) {
		$a = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$a[$data_array[$i][$key]][] = $data_array[$i];
		}
		
		return $a;
	}
	
	function getListKeyField($data_array, $key, $field) {
		$a = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$a[$data_array[$i][$key]] = $data_array[$i][$field];
		}
		
		return $a;
	}
	
	function getListKeyFields($data_array, $key, $field) {
		$a = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$a[$data_array[$i][$key]][] = $data_array[$i][$field];
		}
		
		return $a;
	}
	
	function setListKey($data_array, $key) {
		$a = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$a[$data_array[$i][$key]] = $data_array[$i];
		}
		
		return $a;
	}
	
	function setListKeyField($a, $b, $key, $field) {
		for($i=0, $n=sizeof($a); $i<$n; $i++) {
			$a[$i][$field] = $b[$a[$i][$key]];
		}
		return $a;
	}
	
	function setListKeyFields($a, $b, $key, $field, $keyb = '', $fielda = '') {
		if( $keyb != '' ) {
			$b = $this->setListKey($b, $keyb);
		}
		if( $fielda == '' ) {
			$fielda = $field;
		}
		for($i=0, $n=sizeof($a); $i<$n; $i++) {
			$a[$i][$fielda] = $b[$a[$i][$field]];
		}
		return $a;
	}
	
	function sortListKey($a, $key) {
		$a = $this->setListKey($a, $key);
		ksort($a);
		$a = array_values($a);
		
		return $a;
	}
	
	function sumListField($a, $field) {
		$s = 0;
		for($i=0, $n=sizeof($a); $i<$n; $i++) {
			$s += $a[$i][$field];
		}
		
		return $s;
	}
	
	//link
	function _filter_link($link) {
		$link = stripslashes($link);
		//$link = trim(ereg_replace("[^A-Za-z0-9:#=_\!\@\%\$\^\&]", " ", $link));
		//$link = trim(ereg_replace("[^A-Za-z0-9:#=_\!\@\%]", " ", $link));
		//$link = trim(ereg_replace("( {1,})", " ", $link));
		//$link = ereg_replace(" ", "_", $link);
		//$link = strtolower($link);
		$link = trim(ereg_replace("[^A-Za-z0-9-]", " ", $link));
		$link = ereg_replace("( {1,})", "-", $link);
		
		return $link;
	}
	
	function _rand_link($a = '', $b = '') {
		$p = '';
		if( $this->_not_null($b) == false || strlen($b) > 50 ) {
			$p = $a;
		} else {
			$p = $b;
		}
		return (($p == '') ? '' : $p . '_') . $this->_rand_value(5);
	}
	
	//post
	function _post_field($d, $a, $b = 'field', $check = false) {
		if( is_array($d) ) {
			$p = array();
			foreach($d as $k => $v) {
				if( strlen($b) > 0 && substr($k, 0, strlen($b)) == $b ) {
					$p[$a . substr($k, strlen($b))] = $v;
				} elseif( substr($k, 0, 1) == '_' ) {
					$p[$a . substr($k, 1)] = $v;
				} elseif( substr($k, 0, 1) == '=' ) {
					$p[substr($k, 1)] = $v;
				} elseif( $check == false ) {
					$p[$k] = $v;
				}
			}
			
			return $p;
		} else {
			return $d;
		}
	}
	
	function _post_value($d, $a = '', $b = 'field', $check = false) {
		if( is_array($d) ) {
			$p = array();
			foreach($d as $k => $v) {
				if( strlen($b) > 0 && substr($v, 0, strlen($b)) == $b ) {
					$p[$k] = $a . substr($v, strlen($b));
				} elseif( $check == false ) {
					$p[$k] = $v;
				}
			}
			
			return $p;
		} else {
			return $d;
		}
	}
	
	//private
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _rand($min, $max) {
		return util_create_random($min, $max);
	}
	
	function _rand_value($length, $type = 'fullchars') {
		return util_create_random_value($length, $type);
	}
	
	//type
	//	0	int/number
	//	1	string
	function _implode($s, $a, $t = 0) {
		if( is_array($a) ) {
			$a = array_values($a);
			$b = array();
			for($i=0, $n=sizeof($a); $i<$n; $i++) {
				if( $t == 0 && $a[$i] > 0 ) {
					$b[] = $a[$i];
				} elseif( $t == 1 ) {
					$b[] = $a[$i];
				}
			}
			$b = array_unique($b);
			if( sizeof($b) > 0 ) {
				return implode($s, $b);
			}
		}
		
		return 0;
	}
	
	function _implode_in($a) {
		$b = $this->_implode(',', $a);
		if( $b == '0' ) {
			return '(0)';
		}
		return '(' . $b . ')';
	}
	
	function _explode($s, $a) {
		$c = array();
		$b = explode($s, $a);
		if( is_array($b) ) {
			for($i=0, $n=sizeof($b); $i<$n; $i++) {
				if( $b[$i] > 0 ) {
					$c[] = $b[$i];
				}
			}
		}
		
		return $c;
	}
	
	function _rands($count, $length) {
		if( $count >= $length ) {
			$count = $length;
		}
		$rands = array();
		while( sizeof($rands) < $count ) {
			$rand = $this->_rand(0, $length-1);
			if( !in_array($rand, $rands) ) {
				$rands[] = $rand;
			}
		}
		
		return $rands;
	}
	
	function _rows_rands($rows, $count) {
		$length = sizeof($rows);
		$rands = $this->_rands($count, $length);
		
		return $this->_rows_in_rands($rows, $rands);
	}
	
	function _rows_in_rands($rows, $rands) {
		$rows_rands = array();
		for($i=0, $n=sizeof($rands), $m=sizeof($rows); $i<$n && $i<$m; $i++) {
			$rows_rands[] = $rows[$rands[$i]];
		}
		
		return $rows_rands;
	}
	
	function _array_keys($data_array, $key) {
		$keys = array();
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$keys[] = $data_array[$i][$key];
		}
		
		return $keys;
	}
	
	function _array_tolisthtml($data_array, $js, $keys, $class = 'v') {
		$html = '<ul class="' . $class . '">';
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$html .= '<li ' . $js . '>';
			if( is_array($keys) ) {
				foreach($keys as $k => $key) {
					$html .= '<span class="' . $k . '">' . $data_array[$i][$key] . '</span>';
				}
			} else {
				$html .= '<span class="' . $keys . '">' . $data_array[$i][$keys] . '</span>';
			}
			$html .= '</li>';
		}
		$html .= '</ul>';
		
		return $html;
	}
	
	function _select_options($data_array, $key, $field, $name, $parameter = '', $default = '') {
		$html = '';
		$html .= '<select name="' . $name . '" ' . $parameter . '>';
		if( is_array($default) ) {
			$html .= '<option value="' . $default['value'] . '">' . $default['text'] . '</option>';
		} elseif( $default != '' ) {
			$html .= '<option value="">' . $default . '</option>';
		}
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$html .= '<option value="' . $data_array[$i][$key] . '">' . $data_array[$i][$field] . '</option>';
		}
		$html .= '</select>';
		
		return $html;
	}
	
	//
	function _check_time($timea, $timeb = '', $compare = 'start_end') {
		$timea = util_mktime($timea);
		if( $timeb == 'now' ) {
			$timeb = time();
		} else {
			$timeb = util_mktime($timeb);
		}
		if( $compare == 'start_end' || $compare == 'up_down' ) {
			if( $timeb > 0 && $timea >= $timeb ) {
				return -1;
			}
		} elseif( $compare == 'expire' || $compare == 'between' || $compare == 'in' ) {
			$now = time();
			if( $timea >= $now ) {
				return -1;
			} elseif( $timeb <= $now ) {
				return -2;
			}
		}
		
		return 1;
	}
	
	function _is_expire($timea, $timeb) {
		$result = $this->_check_time($timea, $timeb, 'expire');
		
		if( $result < 0 ) {
			return true;
		} else {
			return false;
		}
	}
	
	function _date_time($time = '') {
		if( $time == '' ) {
			return strftime("%Y-%m-%d");//%Y-%m-%d %H:%M:%S
		} elseif( $time == 'today' ) {
			return strftime("%Y-%m-%d");
		} elseif( $time == 'last_day' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m"), date("d")-1, date("Y")));
		} elseif( $time == 'next_day' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
		} elseif( $time == 'last_month' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
		} elseif( $time == 'next_month' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
		} elseif( $time == 'first_month' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, 1, date("d"), date("Y")));
		} elseif( $time == 'last_year' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
		} elseif( $time == 'next_year' ) {
			return strftime("%Y-%m-%d", mktime(0, 0, 0, date("m"), date("d"), date("Y")+1));
		} elseif( $time > 0 ) {
			return strftime("%Y-%m-%d %H:%M:%S", $time);
		} else {
			return strftime("%Y-%m-%d");
		}
	}
	
	function _filter_date($date) {
		if( util_checkdate($date, "yyyy-mm-dd", $dateb) ) {
			$year = $dateb['0'];
			$month = $dateb['1'];
			$day = $dateb['2'];
			
			return $year . '-' . (($month < 10) ? ('0' . $month) : $month) . '-' . (($day < 10) ? ('0' . $day) : $day);
		}
		
		return '';
	}
	
	//
	function _get_current_user() {
		return $_SESSION['suser']->userid;
	}
	
	function _check_user_authority() {
		if( $_SESSION['suser']->userid > 0 ) {
			return true;
		} else {
			return false;
		}
	}
	
	function _get_session_id() {
		global $sessionObj;
		
		if( $sessionObj->session_id ) {
			$sid = $sessionObj->session_id;
		} else {
			$sid = $GLOBALS['PPSID'];
			//$sid = $_COOKIE['PPSID'];
		}
		
		return $sid;
	}
	
	function _query_owner($str) {
		if( $this->owner >= 0 ) {
			return $str . " = '" . $this->owner . "' ";
		}
		
		return ' 1 ';
	}
	
	function _query_check($str, $check) {
		if( $check == -1 ) {
			//
		} else {
			return $str;
		}
		
		return '';
	}
	
	function _query_owner_rt() {
		if( $this->_table_owner_usable == true ) {
			return $this->_query_owner($this->prefix . $this->_table_owner_field);
		}
		return ' 1 ';
	}
	
	function _get_array_key($data_array) {
		$key = '';
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				if( is_array($v) ) {
					$key .= $k . '-' . $this->_get_array_key($v);
				} elseif( is_object($v) ) {
					$key .= $k . '-' . $this->_get_obj_key($v);
				} else {
					$key .= $k . '-' . $v . '--';
				}
			}
		} else {
			$key .= $data_array;
		}
		
		return $key;
	}
	
	function _get_array_keys($data_array) {
		$key = $this->_get_array_key($data_array);
		
		return md5($key);
	}
	
	function _get_obj_key($obj, $excludes = '') {
		if( is_object($obj) ) {
			$data_array = get_object_vars($obj);
			if( $excludes != '' ) {
				if( !is_array($excludes) ) {
					$excludes = array($excludes);
				}
				if( is_array($excludes) ) {
					if( is_array($data_array) ) {
						foreach($data_array as $k => $v) {
							if( in_array($k, $excludes) ) {
								unset($data_array[$k]);
							}
						}
					}
				}
			}
			
			return $this->_get_array_key($data_array);
		}
		
		return $obj;
	}
	
	function _get_key_array($key) {
		$data_array = array();
		if( strstr($key, '--') === false ) {
			return $key;
		}
		$a = explode('--', $key);
		foreach($a as $k => $v) {
			$va = explode('-', $v);
			if( $va['0'] ) {
				$data_array[$va['0']] = $va['1'];
			}
		}
		
		return $data_array;
	}
}
//
?>